package org.lzy.shop.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import org.lzy.shop.R
import org.lzy.shop.response.HomeClassifyTree


class ClassifyAdapter: BaseQuickAdapter<HomeClassifyTree, QuickViewHolder>() {

     private var selectedPosition = 0

    //再定义一个int类型的返回值方法
    fun getPosition(): Int {
        return selectedPosition
    }

    //   传入点击的位置
    fun setPosition(Position: Int) {
        this.selectedPosition = Position
        notifyDataSetChanged()
    }


    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
        return QuickViewHolder(R.layout.item_classify_one, parent)
    }

    override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: HomeClassifyTree?) {
        item?.let {
            holder.setText(R.id.tv_name, item.name)
            // 根据选中状态显示/隐藏蓝色竖线
            val blueLine = holder.getView<View>(R.id.view)
            blueLine.visibility = if (position == selectedPosition) View.VISIBLE else View.GONE
            // 设置选中项的文字颜色
            holder.setTextColor(R.id.tv_name, if (position == selectedPosition) {
                context.resources.getColor(R.color.colorPrimary)
            } else {
                context.resources.getColor(R.color.day_textcolor)
            })
        }

    }
}